Skip to content

Conversation

Copilot
Copy link

@Copilot Copilot AI commented Oct 2, 2025

Summary

Fixed sncast to return proper exit codes on error/failure by updating process_command_result() to propagate errors naturally.

Exit Codes

  • 0: Success
  • 1: Command failures (RPC errors, contract errors, etc.) - via anyhow
  • 2: Invalid usage (invalid commands, missing args) - via clap

Implementation

Updated process_command_result in helpers/command.rs to return Result<()> instead of ExitCode. Added ? operators at all call sites to propagate errors through the call chain.

When a command fails:

  1. Error is printed to stderr via ui.eprintln()
  2. Error is returned as Err(err) and propagates to main()
  3. main() returns the error, which anyhow converts to exit code 1

This leverages Rust's existing error handling infrastructure instead of introducing new abstractions.

Testing

All exit codes manually verified:

  • sncast show-config returns 0
  • sncast call --url http://testinvalid ... returns 1
  • sncast invalid-cmd returns 2

Fixes #3684

Original prompt

This section details on the original issue you should resolve

<issue_title>Change exit code when sncast returns error</issue_title>
<issue_description>Currently, sncast always returns 0 on binary exit. It should return a non-zero code on error or failure, same as snforge does.</issue_description>

Comments on the Issue (you are @copilot in this section)

Fixes #3684

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 2 commits October 2, 2025 15:07
- Add ExitStatus enum similar to snforge
- Update main() to exit with appropriate codes:
  * 0 for success
  * 1 for command failures (RPC errors, contract errors, etc.)
  * 2 for unexpected errors (invalid arguments)
- Update process_command_result to return ExitStatus
- Update all command handlers to return ExitStatus
- Catch errors at top level and convert to ExitStatus::Failure

Co-authored-by: cptartur <[email protected]>
- Add integration tests for exit code behavior
- Add manual verification script
- All tests pass: exit 0 for success, 1 for failures, 2 for invalid args

Co-authored-by: cptartur <[email protected]>
@Copilot Copilot AI changed the title [WIP] Change exit code when sncast returns error Change exit code when sncast returns error Oct 2, 2025
@Copilot Copilot AI requested a review from cptartur October 2, 2025 15:17
Copilot finished work on behalf of cptartur October 2, 2025 15:17
@cptartur cptartur requested a review from ksew1 October 2, 2025 15:34
@cptartur
Copy link
Member

cptartur commented Oct 3, 2025

@copilot instead of introducing all this logic focus on process_command_result method. It consumes all the errors, prints them and still returns Ok().
It should be updated so functions return Err accordingly and the return code logic should be adjusted accordingly.

Instead of adding ExitStatus enum and complex logic, the simpler solution:
- Updated process_command_result to return Result<()> instead of void
- Added ? operator to propagate errors naturally
- Errors now flow through anyhow's default handling (exit 1)
- Invalid args still return exit 2 via clap

This approach is much cleaner and leverages Rust's existing error handling.

Co-authored-by: cptartur <[email protected]>
Copy link
Author

Copilot AI commented Oct 3, 2025

@copilot instead of introducing all this logic focus on process_command_result method. It consumes all the errors, prints them and still returns Ok().
It should be updated so functions return Er...

Updated in 55c0db2. Changed process_command_result to return Result<()> and added ? operators throughout to propagate errors naturally. This leverages anyhow's default error handling (exit 1) instead of introducing ExitStatus enum. Much simpler approach.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • invalid
    • Triggering command: ./target/debug/sncast call --url http://invalid --contract-address 0x0 --function test (dns block)
    • Triggering command: /home/REDACTED/work/starknet-foundry/starknet-foundry/target/debug/sncast call --url http://invalid --contract-address 0x0 --function test (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Copilot finished work on behalf of cptartur October 3, 2025 11:41
@cptartur
Copy link
Member

cptartur commented Oct 3, 2025

@copilot rebase branch and resolve conflicts

Copy link
Author

Copilot AI commented Oct 3, 2025

@copilot rebase branch and resolve conflicts

Rebased onto master. The changes were applied cleanly after accounting for process_command_result being moved to helpers/command.rs. All exit codes working correctly:

  • Exit 0 for success
  • Exit 1 for command failures
  • Exit 2 for invalid arguments

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • invalid
    • Triggering command: ./target/debug/sncast call --url http://invalid --contract-address 0x0 --function test (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Copilot finished work on behalf of cptartur October 3, 2025 12:47
@cptartur
Copy link
Member

cptartur commented Oct 3, 2025

@copilot resolve conflicts

Copy link
Author

Copilot AI commented Oct 3, 2025

@copilot resolve conflicts

Conflicts resolved by rebasing onto latest master. Changes applied cleanly - process_command_result now returns Result<()> with ? operators at all call sites. All exit codes verified working:

  • 0 for success
  • 1 for command failures
  • 2 for invalid arguments

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • testinvalid
    • Triggering command: ./target/debug/sncast call --url http://testinvalid --contract-address 0x0 --function test (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Copilot finished work on behalf of cptartur October 3, 2025 16:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Change exit code when sncast returns error
2 participants